package com.sino.androiddesign.customView


import android.content.Context
import android.graphics.*
import android.os.Bundle
import android.support.v4.app.Fragment
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

import com.sino.androiddesign.R


class PathFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_path, container, false)
    }


}

/**
 * 路径及文字
 * 参考:https://blog.csdn.net/harvic880925/article/details/38926877
 */

class PathView:View{

    constructor(context: Context):this(context,null)

    constructor(context: Context, attrs: AttributeSet?):super(context,attrs)

    private val mPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG)  //开启抗锯齿

    private val path: Path = Path()  //直线路径

    init {
        mPaint.style = Paint.Style.STROKE   //描边
        mPaint.strokeWidth = 5f

    }


    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        path.moveTo(10f,10f)
        path.lineTo(10f,100f)
        path.lineTo(300f,100f)
        path.lineTo(300f,10f)
        path.close()   //close 会将首尾点 相连 形成一个闭环
        mPaint.color= Color.RED
        canvas!!.drawPath(path,mPaint)   //绘制路径

        //1 两者的作用主要是用来处理 文字的排版
        val ccwReactPath = Path()  //逆时针生成
        val ccwRect1 = RectF(200f, 200f, 500f, 500f)
        ccwReactPath.addRect(ccwRect1, Path.Direction.CCW)

        val cwReactPath = Path()  //顺时针生成
        val cwRect2 = RectF(600f, 200f, 800f, 500f)
        cwReactPath.addRect(cwRect2, Path.Direction.CW)

        canvas.drawPath(ccwReactPath,mPaint)
        canvas.drawPath(cwReactPath,mPaint)

        //2 依据路径写出文字
        val text ="hello world!"
        mPaint.style= Paint.Style.FILL
        mPaint.color = Color.RED
        mPaint.textSize = 40f
        //hOffset 离起点的距离  vOffset 离path的距离
        canvas.drawTextOnPath(text,ccwReactPath,40f,30f,mPaint)
        canvas.drawTextOnPath(text,cwReactPath,0f,10f,mPaint)

        /**
         * 3: 圆角矩形路径
         */
        val roundPath1 = Path();
        val rectF1 = RectF(100f,600f,400f,700f)
        //这个 只能构建唯一相同角度 的圆角
        roundPath1.addRoundRect(rectF1,10f,15f, Path.Direction.CCW)

        val  rect2 = RectF(500f,600f,700f,700f)
        val  raddii :FloatArray= floatArrayOf(10f,15f,20f,25f,30f,25f,40f,45f)
        //(x1,y1) 对应第一个角(左上角,顺时针) 必须八个参数 对应不同的radius
        roundPath1.addRoundRect(rect2,raddii, Path.Direction.CCW)

        canvas.drawPath(roundPath1,mPaint)

        //4 圆形路径
        val circlePath = Path()
        circlePath.addCircle(100f,800f,50f, Path.Direction.CCW)
        canvas.drawPath(circlePath,mPaint)

        //5 ;椭圆路径

        val ovalPath = Path()
        val ovalRecF = RectF(300f,750f,400f,850f)
        ovalPath.addOval(ovalRecF, Path.Direction.CCW)
        canvas.drawPath(ovalPath,mPaint)

        //6: 弧
        // 弧是椭圆的一部分 因为椭圆是根据矩形生成,所以弧也是根据矩形生成
        // 相对于矩形生成的椭圆 ,其圆点就是中心点 以中心点为坐标圆点,画出角度(x轴正向 为0 )
        mPaint.style = Paint.Style.STROKE

        val arcRect1 = RectF(500f,750f,600f,850f)
        canvas.drawArc(arcRect1,0f,-180f,true,mPaint)
        canvas.drawArc(arcRect1,0f,90f,true,mPaint)

        val arcRect2 = RectF(700f,750f,800f,850f)
        canvas.drawArc(arcRect2,0f,90f,false,mPaint)


        val arcPath = Path()
        val arc1 = RectF(100f,900f,400f,1000f)
        arcPath.addArc(arc1,0f,100f)
        canvas.drawPath(arcPath,mPaint)



    }

}
